/**
 * 
 */
package com.effectiv.gooruda.security;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import com.effectiv.gooruda.domain.Lov;
import com.effectiv.gooruda.domain.Tenant;

import com.effectiv.gooruda.repository.TenantRespository;

/**
 * @author Dhrubo
 * 
 */
@Component
public class SimpleUserDetailsService implements UserDetailsService {

	@Autowired
	private TenantRespository tenantRespository;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.security.core.userdetails.UserDetailsService#
	 * loadUserByUsername(java.lang.String)
	 */
	
	public UserDetails loadUserByUsername(String email)
			throws UsernameNotFoundException {

		Tenant tenant = tenantRespository.findByEmail(email);

		Lov role = tenant.getRole();

		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		GrantedAuthority ga = new SimpleGrantedAuthority(role.getName());
		authorities.add(ga);

		SimpleUserDetails userDetails = new SimpleUserDetails(tenant,
				authorities);

		return userDetails;
	}

}
